﻿using System.Drawing;
using GIMS.Def.Flags;
using GIMS.Def.Operators;
using System.ComponentModel;
using GIMS.Def.Arguments;
using System.IO;

namespace GIMS.GUI.GraphItems
{
    public class CustomVertex : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string previewImagePath;
        private string imageSize;
        private string name;

        public CustomVertex(Operator imOperator)
        {
            this.IMOperator = imOperator;
            if (null != IMOperator)
            {
                IMOperator.PropertyChanged += new PropertyChangedEventHandler(IMOperator_PropertyChanged);
                SetName();
            }
        }

        void IMOperator_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Argument.IsParameter")
            {
                SetName();
            }
        }

        public Operator IMOperator
        {
            get;
            private set;
        }

        public string Name
        {
            get { return name; }
            private set
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }

        public bool HasTopDrop
        {
            get { return IMOperator.Position != Position.First; }
        }

        public bool HasBottomDrop
        {
            get { return IMOperator.Position != Position.Last; }
        }

        public bool HasIsParameter
        {
            get { return IMOperator.Argument != null && IMOperator.Argument.Type == ArgumentType.path; }
        }

        public string PreviewImagePath
        {
            get { return previewImagePath; }
            set
            {
                if (value != previewImagePath)
                {
                    previewImagePath = value;
                    SetImageSize();
                    NotifyPropertyChanged("PreviewImagePath");
                }
            }
        }

        public string ImageSize
        {
            get { return imageSize; }
            private set
            {
                imageSize = value;
                NotifyPropertyChanged("ImageSize");
            }
        }

        private void SetImageSize()
        {
            ImageSize = string.Empty;
            if (File.Exists(PreviewImagePath))
            {
                var image = Image.FromFile(PreviewImagePath);
                ImageSize = string.Concat(image.Width.ToString(), "x", image.Height.ToString());
            }
        }

        private void SetName()
        {
            Name = IMOperator.Name;
            if (IMOperator.Argument!=null && IMOperator.Argument.Type == ArgumentType.path)
            {
                var pathArgument = (PathArgument)IMOperator.Argument;
                if (pathArgument.IsParameter)
                {
                    Name += " - Parameter";
                }
            }
        }

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
